﻿using AntdUI;
using AttendanceSysytem.Database.BusinessLogic;
using AttendanceSysytem.Models;
using Mapster;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AttendanceSysytem.Controls
{
    public partial class EmployeePage : BaseControl
    {
        public EmployeePage()
        {
            InitializeComponent();
            InitTable();
            ReflushTable();
        }

        Employee_Logic logic = new Employee_Logic();

        private void InitTable()
        {

            var Columns = new ColumnCollection();
            Columns.Add(new Column("Name", "姓名"));
            Columns.Add(new Column("Account", "账号"));
            Columns.Add(new Column("TimeRuleText", "考勤规则"));
            Columns.Add(new Column("DepartText", "部门"));
            table1.Columns = Columns;

        }

        private void btnChoose1_Click(object sender, EventArgs e)
        {
            OpenSelectFile(txtEmployeeFile);
        }

        private void OpenSelectFile(Input input)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "EXCEL|*.xlsx;";
            var result = ofd.ShowDialog();
            if (result == DialogResult.OK)
            {
                input.Text = ofd.FileName;
            }
        }

        private void btnSyncEmployee_Click(object sender, EventArgs e)
        {
            string filename = txtEmployeeFile.Text;
            if(! File.Exists(filename))
            {
                MessageBoxShow("员工表格文件不存在", TType.Error);
                return;
            }
            AntdUI.Spin.open(this, new AntdUI.Spin.Config()
            {
                Back = Color.FromArgb(220, 147, 181, 207),
                Color = Style.Db.Primary,//转圈颜色

                Radius = 6,
                Fore = Color.Black,//字体颜色
                Font = new Font("Microsoft YaHei UI", 14f),//字体可以控制进度圈的大小
            }, (config) =>
            {
                config.Value = 0.2f;
                //耗时代码，处理数据
                config.Text = "正在处理中...";
                Thread.Sleep(500);
                
                SyncEmployeeData();
                ReflushTable();

                config.Value = 1.0f;
                config.Text = "处理完成!";
                Thread.Sleep(500);
            }, () =>
            {
                //结束时执行的代码
            });

        }

        private void ReflushTable()
        {
            var all = logic.AllList();
            table1.DataSource = all;

        }

        private void SyncEmployeeData()
        {
            string filename = txtEmployeeFile.Text;
            var rows1 = MiniExcel.Query<ImportEmployeeTable>(filename, startCell: "A10");
            var all = logic.AllList();

            //
            foreach (var row in rows1)
            {
               var emp = row.Adapt<Employees>();
                var one = all.Find(a=>a.Account == emp.Account);
                if (one != null)
                {
                    emp.Id = one.Id;
                    emp.TimeRule = one.TimeRule;
                }
                else
                {
                    //若部门包含 生产部  考勤规则默认为车间规则
                    if (emp.DepartText.Contains("生产部"))
                        emp.TimeRule = 2;
                }
                if (emp.Id > 0)
                    logic.UpdateSync(emp);
                else
                    logic.Insert(emp);


            }

        }


    }
}
